package com.client.ryt.task.http.response;


import com.client.ryt.basic.BaseResponse;
import com.client.ryt.entity.biz.TradeRecordEntity;
import com.client.ryt.entity.exception.JsonException;
import com.client.ryt.entity.exception.RemoteDataException;
import com.client.ryt.utils.DateUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2014/7/12.
 */
public class TradeRecordResponse extends BaseResponse {

    List<TradeRecordEntity> list = new ArrayList<TradeRecordEntity>();
    int pagesize;
    int pageindex;
    int recordcount;
    int pagecount;

    public int getPagesize() {
        return pagesize;
    }

    public int getPageindex() {
        return pageindex;
    }

    public int getRecordcount() {
        return recordcount;
    }

    public int getPagecount() {
        return pagecount;
    }

    public List<TradeRecordEntity> getList() {
        return list;
    }

    public TradeRecordResponse(){super(null);}
    public TradeRecordResponse(JSONObject json) {
        super(json);
        try {
            if(this.getIsSuccess()){
                JSONObject msg = json.getJSONObject("msg");
                pagesize = msg.getInt("pagesize");
                pagecount = msg.getInt("pagecount");
                pageindex = msg.getInt("pageindex");
                recordcount = msg.getInt("recordcount");
                JSONArray jr = msg.getJSONArray("list");
                TradeRecordEntity tradeRecordEntity = null;
                JSONObject jo = null;
                for(int i=0;i<jr.length();i++){
                    tradeRecordEntity = new TradeRecordEntity();
                    jo= jr.getJSONObject(i);
                    tradeRecordEntity.setRunningNumber("SN："+jo.getString("buy_serial"));
                    tradeRecordEntity.setTradeDate(jo.getString("ct"));
                    tradeRecordEntity.setPayInfo(jo.getString("card"));
                    tradeRecordEntity.setId(jo.getString("id"));
                    JSONArray goods = jo.getJSONArray("goods");
                    String goodsString = "";
                    for (int n=0;n<goods.length();n++){
                        JSONObject joo = goods.getJSONObject(n);
                        goodsString += joo.getString("title")+",";
                    }
                    if(goodsString.indexOf(",") != -1) goodsString = goodsString.substring(0,goodsString.length()-1);
                    tradeRecordEntity.setTradeItem(goodsString);
                    list.add(tradeRecordEntity);
                }
            }else{
                this.setBaseException(new RemoteDataException(json));
            }
        } catch (JSONException e) {
            this.setBaseException(new JsonException(e.getMessage()));
        }
    }

}
